package chpt03;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

class Person3 implements Comparable<Person3> {
	private String name = null;
	private int age;
	private boolean gender;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public boolean isGender() {
		return gender;
	}

	public void setGender(boolean gender) {
		this.gender = gender;
	}

	public Person3(String name, int age, boolean gender) {
		this.name = name;
		this.gender = gender;
		this.age = age;
	}

	@Override
	public String toString() {
		return name + "-" + age + "-" + gender;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Person3 other = (Person3) obj;
		if (age != other.age)
			return false;
		if (gender != other.gender)
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	@Override
	public int compareTo(Person3 o) {
		if (this.name.compareTo(o.name) < 0)
			return -1;
		else if (this.name.compareTo(o.name) > 0)
			return 1;
		else if (this.age < o.age)
			return -1;
		else if (this.age > o.age)
			return 1;
		else
			return 0;
	}

}

class Student3 extends Person3 {
	private String stuNo = null;
	private String clazz = null;

	public String getStuNo() {
		return stuNo;
	}

	public void setStuNo(String stuNo) {
		this.stuNo = stuNo;
	}

	public String getClazz() {
		return clazz;
	}

	public void setClazz(String clazz) {
		this.clazz = clazz;
	}

	public Student3(String name, int age, boolean gender, String stuNo, String clazz) {
		super(name, age, gender);
		this.stuNo = stuNo;
		this.clazz = clazz;
	}

	@Override
	public String toString() {
		return "Student:" + super.toString() + "-" + stuNo + "-" + clazz;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Student3 other = (Student3) obj;
		if (clazz == null) {
			if (other.clazz != null)
				return false;
		} else if (!clazz.equals(other.clazz))
			return false;
		if (stuNo == null) {
			if (other.stuNo != null)
				return false;
		} else if (!stuNo.equals(other.stuNo))
			return false;
		return true;
	}

}

class Company4 {
	private String name = null;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Company4(String name) {
		super();
		this.name = name;
	}

	@Override
	public String toString() {
		return name;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Company4 other = (Company4) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

}

class Employee3 extends Person3 {
	private double salary;
	private Company4 company;

	public Company4 getCompany() {
		return company;
	}

	public void setCompany(Company4 company) {
		this.company = company;
	}

	public double getSalary() {
		return salary;
	}

	public void setSalary(double salary) {
		this.salary = salary;
	}

	public Employee3(String name, int age, boolean gender, double salary, Company4 company) {
		super(name, age, gender);
		this.salary = salary;
		this.company = company;
	}

	@Override
	public String toString() {
		return "Employee:" + super.toString() + "-" + company + "-" + salary;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (!super.equals(obj))
			return false;
		if (getClass() != obj.getClass())
			return false;
		Employee3 other = (Employee3) obj;
		if (company == null) {
			if (other.company != null)
				return false;
		} else if (!company.equals(other.company))
			return false;
		DecimalFormat df = new DecimalFormat("#.#");
		if (!df.format(salary).equals(df.format(other.salary))) {
			return false;
		}
		return true;
	}

}

public class Person_Inherit_Override {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int flag = 0;
		ArrayList<Person3> personlist = new ArrayList<Person3>();
		while (in.hasNext() && flag == 0) {
			String str = in.next();
			if (str.equals("s")) {
				Person3 student = new Student3(in.next(), in.nextInt(), in.nextBoolean(), in.next(), in.next());
				personlist.add(student);
			} else if (str.equals("e")) {
				String n = in.next();
				int a = in.nextInt();
				boolean g = in.nextBoolean();
				double s = in.nextDouble();
				Company4 c = new Company4(in.next());
				Person3 employee = new Employee3(n, a, g, s, c);
				personlist.add(employee);
			} else {
				Collections.sort(personlist);
				for (Person3 person : personlist) {
					System.out.println(person);
				}
				if (in.next().equals("exit"))
					return;

				ArrayList<Student3> stulist = new ArrayList<Student3>();
				ArrayList<Employee3> emplist = new ArrayList<Employee3>();
				for (Person3 person : personlist) {
					if (person instanceof Student3&&!stulist.contains(person)) {
						stulist.add((Student3) person);
					} else if (person instanceof Employee3&&!emplist.contains(person)) {
						emplist.add((Employee3) person);
					}
				}
				System.out.println("stuList");
				for (Student3 student : stulist) {
					System.out.println(student);
				}
				System.out.println("empList");
				for (Employee3 employee : emplist) {
					System.out.println(employee);
				}

				break;
			}
			in.close();
		}
		
	}

}
